﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core;
using Cofe.Core.EntryType;
using Cofe.Core.Implements;
using Cofe.Web.Utils;
using Newtonsoft.Json.Linq;

namespace Cofe.Web
{
    [Export(typeof(ICofeService))]
    [ServicePriority(ServicePriorityAttribute.DefaultPriority_COFEWS)]
    public class WSEntryJObjectSerializer : EntryJObjectSerializer
    {
        protected override object getValue(CofeSystemInfo entry, object prop)
        {
            switch ((int)prop)
            {
                case (int)CofeProperties.FullParseName:
                case (int)CofeProperties.ParentParseName:
                    return ((string)base.getValue(entry, prop)).Replace('\\', '/');
            }

            return base.getValue(entry, prop);
        }

        public override JObject WriteEntry<T>(T entry, bool includeDetails = false, bool writeLinks = true)
        {
            var retVal = base.WriteEntry<T>(entry, includeDetails);
            if (writeLinks)
            {
                IEnumerable<XmlLink> links = UrlUtils.CreateLinks<T>(entry.Properties, null, includeDetails);
                if (links.Any())
                    retVal.Add("links", xmlLinkToJArray(links));
            }
            return retVal;
        }

        public override JObject WriteEntryList(WebEntryList entryList, bool includeDetails = false, bool writeLinks = true)
        {
            var retVal = base.WriteEntryList(entryList, includeDetails);
            if (writeLinks)
            {
                IEnumerable<XmlLink> links = UrlUtils.CreateParameterDicLinks(entryList.Parameters);
                if (links.Any())
                    retVal.Add("links", xmlLinkToJArray(links));
            }
            return retVal;
        }
    }
}
